<?php

/**
 * file containing administrator function for the jfusion plugin
 *
 * PHP version 5
 *
 * @version    $Id: ###FILE_NAME### ###REVISION### $
 * @category   JFusion
 * @package    JFusionPlugins
 * @subpackage copix
 * @author     Emmanuel DENIS
 * @copyright  ###COPYRIGHT###
 * @license    ###LICENSE###
 * @link       http://www.jfusion.org
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

/**
 * JFusion Admin Class for copix
 * For detailed descriptions on these functions please check the model.abstractadmin.php
 *
 * @category   JFusion
 * @package    JFusionPlugins
 * @subpackage copix
 * @author     Emmanuel DENIS
 * @copyright  ###COPYRIGHT###
 * @license    ###LICENSE###
 * @link       http://www.jfusion.org
 */
class JFusionAdmin_copix extends JFusionAdmin {
    public $exclude_admins;
    /**
     * returns the name of this JFusion plugin
     * @return string name of current JFusion plugin
     */
    function getJname() {
        return 'copix';
    }
    function __construct() {
        $params = JFusionFactory::getParams($this->getJname());
        if( $params->get('select_users_only') == true ) {
            $this->exclude_admins = true;
        } else {
            $this->exclude_admins = false;
        }
    }
    function getTablename() {
        return 'dbuser';
    }
    function loadSetup($path) {
        if (!$path) {
            $params = JFusionFactory::getParams($this->getJname());
            $path = $params->get('source_path');
        }
        if (substr($path, -1) != DS) {
            $chemin = $path.DS;
        } else {
            $chemin = $path;
        }
        $file = $chemin.'var'.DS.'config'.DS.'db_profiles.conf.php';
        if (file_exists($file)) {
            require ($file);
            return array($_db_default_profile, $_db_profiles);
        }
    }
    function setupFromPath($path) {
        list($copix_db_default_profile, $copix_db_profiles) = JFusionAdmin_copix::loadSetup($path);
        if( !empty($copix_db_default_profile) && !empty($copix_db_profiles) ){
            $current_profile = $copix_db_profiles[$copix_db_default_profile];
            //save the parameters into array
            $params = array();
            $params['database_host'] = $copix_db_default_profile;
            // la chaine de connection est de la forme 'dbname=ma_base'
            $morceaux = explode('=',$current_profile['connectionString']);
            if( !empty($morceaux[1]) ){
                $params['database_name'] = $morceaux[1];
            }
            $params['database_user'] = $current_profile['user'];
            $params['database_password'] = $current_profile['password'];
            $params['database_prefix'] = "";
            $params['database_type'] = "mysql";
            $params['source_path'] = $path;
            return $params;
        } else {
            JError::raiseWarning(500, JText::_('WIZARD_FAILURE') . ": $path " . JText::_('WIZARD_MANUAL'));
            $result = false;
            return $result;
        }
    }
    function getUserList($start = 0, $count = '') {
        //getting the connection to the db
        $db = JFusionFactory::getDatabase($this->getJname());
        $query  = 'SELECT login_dbuser AS username, email_dbuser AS email';
        $query .= ' FROM '.$this->getTablename();
        if ($this->exclude_admins) {
            $query .= ' WHERE id_dbuser NOT IN (SELECT user_dbgroup FROM dbgroup_users)';
        }
        if (!empty($count)) {
            $query.= ' LIMIT ' . $start . ', ' . $count;
        }
        $db->setQuery($query);
        //getting the results
        $userlist = $db->loadObjectList();
        return $userlist;
    }
    function getUserCount() {
        //getting the connection to the db
        $db = JFusionFactory::getDatabase($this->getJname());
        $query  = 'SELECT count(*)';
        $query .= ' FROM '.$this->getTablename();
        if ($this->exclude_admins) {
            $query .= ' WHERE id_dbuser NOT IN (SELECT user_dbgroup FROM dbgroup_users)';
        }
        $db->setQuery($query);
        //getting the results
        return $db->loadResult();
    }
    function getUsergroupList() {
        $default_group = new stdClass;
        $default_group->name = 'Utilisateur';
        $default_group->id = '0';
        $groups_list[] = $default_group;
        return $groups_list;
    }
    function getDefaultUsergroup() {
        // a standard Copix user has no group but JFusion and Joomla need one
        return 'Utilisateur';
    }
    function allowRegistration() {
        return false;
    }
    function allowEmptyCookiePath() {
        return true;
    }
    function allowEmptyCookieDomain() {
        return true;
    }
}
